package com.linkage.educloud.ah.adapter.face;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.util.FaceUtils;

import java.util.List;

/**
 * Created by Mr'Tang on 2017/6/19.
 */

public class FaceListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<FaceUtils.Face> mFaces;

    public FaceListAdapter(Context context, List<FaceUtils.Face> faces) {
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(mContext);
        this.mFaces = faces;
    }

    @Override
    public int getCount() {
        if(mFaces != null) {
            return mFaces.size();
        }
        return 0;
    }

    @Override
    public FaceUtils.Face getItem(int position) {
        if(mFaces != null) {
            return mFaces.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.chat_face_grid_item, parent, false);
        }

        FaceUtils.Face face = getItem(position);
        ImageView faceImage = (ImageView) convertView.findViewById(R.id.face_image);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 2;
        Bitmap bmp = null;
        bmp = BitmapFactory.decodeResource(mContext.getResources(), face.imageId,
                opts);
        faceImage.setImageBitmap(bmp);
        if (bmp.isRecycled()) {
            bmp.recycle();
        }
        return convertView;
    }

}

